package leetcode.problems;

import org.junit.Test;

import java.util.*;

/**
 * Created by gmwang on 2018/7/25
 * 子域访问计数
 */
public class _0812KeyboardRow {
    /**
     *一个网站领域，如“讨论.LeCaldC.com”由各种子域组成。在顶层，我们有“COM”，在下一个层次，我们有“LeeCald.com”，并且在最低级别，“讨论.LeCald.com”。当我们访问一个像“讨论.LeeCald.com”这样的域名时，我们也会隐式地访问父域“LeeCald.com”和“COM”。
     *
     *
     *
     * 现在，调用一个“计数配对域”作为一个计数（表示这个域接收到的访问次数），接着是一个空间，接着是地址。计数配对域的一个例子可能是“9001讨论。
     *
     *
     *
     * 我们给出了计数配对域的列表CPN域。我们想要一个计数成对域的列表，（以与输入相同的格式，并且以任何顺序），明确地计算访问每个子域的次数。
     * A website domain like "discuss.leetcode.com" consists of various subdomains. At the top level, we have "com", at the next level, we have "leetcode.com",
     *
     * 一个像"discuss.leetcode.com"这样的域名，包含各种子域名，顶级是"com",下级是"leetcode.com"
     * and at the lowest level, "discuss.leetcode.com". When we visit a domain like "discuss.leetcode.com",
     *
     * 末级是"discuss.leetcode.com"。当我们访问"discuss.leetcode.com"主域名，
     * we will also visit the parent domains "leetcode.com" and "com" implicitly.
     *
     * 我们也会隐式地访问主域名"leetcode.com" 和 "com"
     * Now, call a "count-paired domain" to be a count (representing the number of visits this domain received),
     *
     * 现在，"计数配对域名"是一个计数（代表访问主站的次数）
     * followed by a space, followed by the address. An example of a count-paired domain might be "9001 discuss.leetcode.com".
     *
     * 其次是空间，其次是地址。一个关于计数配对域名的例子"9001 discuss.leetcode.com".
     * We are given a list cpdomains of count-paired domains. We would like a list of count-paired domains,
     * 有一个计数配对域名的集合，
     * (in the same format as the input, and in any order), that explicitly counts the number of visits to each subdomain.
     *
     * Example 1:
     * Input:
     * ["9001 discuss.leetcode.com"]
     * Output:
     * ["9001 discuss.leetcode.com", "9001 leetcode.com", "9001 com"]
     * Explanation:
     * We only have one website domain: "discuss.leetcode.com". As discussed above, the subdomain "leetcode.com" and "com" will also be visited. So they will all be visited 9001 times.
     *
     * Example 2:
     * Input:
     * ["900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"]
     * Output:
     * ["901 mail.com","50 yahoo.com","900 google.mail.com","5 wiki.org","5 org","1 intel.mail.com","951 com"]
     * Explanation:
     * We will visit "google.mail.com" 900 times, "yahoo.com" 50 times, "intel.mail.com" once and "wiki.org" 5 times.
     * 、
     * For the subdomains, we will visit "mail.com" 900 + 1 = 901 times, "com" 900 + 50 + 1 = 951 times, and "org" 5 times.
    /**
     *
     * @param cpdomains
     * @return
     */
    public List<String> subdomainVisits(String[] cpdomains) {
        List<String > list = new ArrayList<>();
        Map<String, Integer> resultMap = new HashMap<>();
        Arrays.asList(cpdomains).stream().forEach(item ->{
            String[] resutls = item.split(" ");
            int num = Integer.parseInt(resutls[0]);
            String domains = resutls[1];
            if(domains.contains(".")){
                System.out.println(domains);
                String[] urls = domains.split("\\.");
                System.out.println(Arrays.toString(urls));
                //判断域名长度
                if(urls.length == 2){
                    resultMap.put(urls[1],resultMap.getOrDefault(urls[1],0)+num);
                }
                if(urls.length == 3){
                    resultMap.put(urls[1]+"."+urls[2],resultMap.getOrDefault(urls[1]+"."+urls[2],0)+num);
                    resultMap.put(urls[2],resultMap.getOrDefault(urls[2],0)+num);
                }
            }
            resultMap.put(domains,resultMap.getOrDefault(domains,0)+num);
        });
        Set<Map.Entry<String, Integer>> set = resultMap.entrySet();
        set.forEach(item ->{
            String domain = item.getKey();
            int num = item.getValue();
            String result = num + " "+domain;
            list.add(result);
        });
        return list;
    }
    @Test
    public void test() {
//        String ints = "hello";
        String[] ints = {"9001 discuss.leetcode.com"};
//        String[] ints = {"900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org"};
        List<String> res = subdomainVisits(ints);
        System.out.println(res);
    }
}
